/*
* @Author: z.p
* @Project: Zdotp
* @Date: 2024年12月30日
* @Description: 使用NLog日志框架, 搭建日志框架
*/

using NLog;

namespace Zdotp.Logs;

public class ZLogger
{
    private readonly Logger _logger;
    
    private static Func<string, LogHelper.Level, LogHelper.Level, NLog.Config.LoggingConfiguration> _logConfig;
    private static LogHelper.Level _minLevel;
    private static LogHelper.Level _maxLevel;
    private static bool _isSetConfig;

    public static void SetConfig(
        Func<string, LogHelper.Level, LogHelper.Level, NLog.Config.LoggingConfiguration> logConfig,
        LogHelper.Level minLevel = LogHelper.Level.Info, LogHelper.Level maxLevel = LogHelper.Level.Fatal)
    {
        _logConfig = logConfig;
        _minLevel = minLevel;
        _maxLevel = maxLevel;
        _isSetConfig = true;
    }
    
    public ZLogger(string loggerName)
    {
        if (_isSetConfig)
        {
            LogManager.Configuration = _logConfig(loggerName, _minLevel, _maxLevel);
        }
        else
        {
            LogManager.Configuration = LogHelper.CreateConsoleLogConfig(loggerName, _minLevel, _maxLevel);
        }
        _logger = LogManager.GetLogger($"{loggerName} logger");
    }
    public void Info(string message) => _logger.Info(message);
    public void Warn(string message) => _logger.Warn(message);
    public void Error(string message) => _logger.Error(message);
    public void Fatal(string message) => _logger.Fatal(message);
    public void Log(LogLevel level, string message) => _logger.Log(level, message);
}